package com.hp.controller;

import com.hp.entity.ResourceTypeTreeVo;
import com.hp.entity.ResourcesType;
import com.hp.result.DataGridResultInfo;
import com.hp.result.SubmitResultInfo;
import com.hp.service.ResourcesTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//resources/querytyperesources
@Controller
@RequestMapping("/resources")
public class ResourcesTypeController {

    @Autowired
    private ResourcesTypeService resourcesTypeService;
    /**
     * 去资源分类页面
     * @return
     */
    @GetMapping("/querytyperesources")
    public String querytyperesources(){
        return "/resourcestype/querytyperesources";
    }

    /**
     * 分页接口
     * @param page
     * @param rows
     * @param resourcesType
     * @return
     */
    @ResponseBody
    @RequestMapping("/querytyperesources_result")
    public DataGridResultInfo querytyperesources_result(@RequestParam(name = "page", defaultValue = "1") int page,
                                                        @RequestParam(name = "rows", defaultValue = "10") int rows, ResourcesType resourcesType) throws Exception {
        return  resourcesTypeService.pageresourcetype(page,rows);
    }

    /**
     * 去添加页面
     * @return
     */
    @GetMapping("/addtype")
    public String toadd(){

        return  "/resourcestype/addresourcestype";
    }

    /**
     * 添加类型
     * @param resourcesType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("addsubmit")
    public SubmitResultInfo addsubmit(ResourcesType resourcesType) throws Exception{
        return resourcesTypeService.savetype(resourcesType);
    }

    /**
     * 去类型修改界面
     * @param id
     * @param map
     * @return
     */
    @GetMapping("/toupdatetype")
    public String toupdate(String id, Map<String,Object>map )throws Exception{
        map.put("psr",resourcesTypeService.findTypeById(id));
        return "/resourcestype/updateresourcesType";
    }

    /**
     * 修改类型
     * @param resourcesType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/updatesubmit")
    public SubmitResultInfo updatesubmit(ResourcesType resourcesType) throws Exception {
        return  resourcesTypeService.updatetype(resourcesType);
    }

    /**
     * 删除
     * @param typeid
     * @return
     */
    @ResponseBody
    @PostMapping("/deletetype")
    public SubmitResultInfo deletetype(String typeid) throws Exception {
        return  resourcesTypeService.deletetype(typeid);
    }




    /**
     * 去科普资源页面
     * @return
     */
    @RequestMapping("/resourcesTree")
    public String resourcesTree() {
        return "resourcestype/resourcesTree";
    }

    /*
     * 生成类型的树
     */
    @RequestMapping("/getResourceTypeTreeVo")
    @ResponseBody
    public List<ResourceTypeTreeVo> getResourceTreeVo() throws Exception {
        List<ResourceTypeTreeVo> treeVos = new ArrayList<ResourceTypeTreeVo>();
        treeVos.add(resourcesTypeService.getResourceTypeTreeVo());
        return treeVos;
    }


    /**
     * 资源树点击右边列表
     * @param typeid
     * @param map
     * @return
     * @throws Exception
     */
    @GetMapping("queryPSRById")
    public String queryPSRById(String typeid, Map<String, Object> map)
            throws Exception {
        map.put("typeid", typeid);
        return "resourcestype/queryresourcebyid";
    }


    @ResponseBody
    @RequestMapping("getAllById")
    public DataGridResultInfo getAllById(String typeid,
                                         @RequestParam(name = "page", defaultValue = "1") int page,
                                         @RequestParam(name = "rows", defaultValue = "10") int rows,
                                         ResourcesType resources) throws Exception {
        return resourcesTypeService.getAllById(typeid, page, rows,
                resources);
    }

}
